package com.spsy.orders.listener;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.spsy.orders.domain.Orders;
import com.spsy.orders.service.IOrdersService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


@Component
@RabbitListener(queues = "lzp_queue_2206a8")//监听的队列名称
public class DirectReceiver4 {

    @Autowired
    private IOrdersService ordersService;


    //executorService是一个线程池
    //private ExecutorService executorService = Executors.newFixedThreadPool(5);


    private ExecutorService executorService = Executors.newSingleThreadExecutor();



    @RabbitHandler
    public void process(String msg, Message message, Channel channel) {
        System.out.println("======>lzp_queue_2206a8 消费者收到消息  : " + msg);



        try {

            this.executorService.submit(new Runnable() {
                @Override
                public void run() {

                    System.out.println("线程池-线程的执行");

                    Map<String,String> map = JSON.parseObject(msg,Map.class);

                    String orderId = map.get("orderId");
                    String orderStatus = map.get("orderStatus");

                    System.out.println("======>lzp_queue_2206a8 消费者收到消息  orderId:" + orderId+" orderStatus:"+orderStatus);


                    Orders orders = Orders.builder().orderId(Long.parseLong(orderId)).orderStatus(orderStatus).build();
                    int updateCount = ordersService.updateOrders(orders);

                    System.out.println("======>lzp_queue_2206a8 消费者收到消息  orderId:" + orderId+" updateCount:"+updateCount);

                    //通过订单号修改数据库

                }
            });


        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
